package com.ttt.adbgui.function;

import com.ttt.adbgui.jcomp.JFrameComponent;
import com.ttt.adbgui.jcomp.JToast;
import com.ttt.adbgui.utils.JavaCMD;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

@Component
@Slf4j
public class MouseListener {

    @Autowired
    private JFrameComponent component;
    @Autowired
    private SelectDevices selectDevices;
    @Autowired
    private SelectComponent select;
    @Autowired
    private SetDevices setDevices;
    @Autowired
    private DocumentList documentList;
    @Autowired
    private Dialog dialog;
    @Autowired
    private TableFill tableFill;
    @Autowired
    private JavaCMD javaCMD;
    @Autowired
    private JToast jToast;


    // 文件表格鼠标事件 触发对话弹窗
    public void fileTable(JTable table){
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int button = e.getButton();
                if (button == MouseEvent.BUTTON3){
                    log.info("文件表格触发右键事件");
                    // 后续优化 按钮可以单独绑定事件，不需要自动关闭当前弹窗
                    JPanel panel = component.getContentPane();
                    dialog.dialogFileTable(panel);
                }else if (e.getClickCount() >= 2){
                    // 重新想表格填充最新的数据
                    String cmd = "cmd /c adb -s 127.0.0.1:7555 shell  cd /ueventd.rc";
                    ArrayList<String> strings = javaCMD.execDefault(cmd);
                    System.out.println(strings);
                    if (strings.get(0).contains("Not a directory")){
                        jToast.showDialog("Not a directory");
                        closeDialog();
                    }else {
                        tableFill.tableDataFile(table,strings);
                    }
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
            }
        });
    }

    // 命令脚本表格
    public void scriptTable(JTable table){
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2){
                    selectDevices.getDevices();
                }
            }
        });
    }

    // 文件管理
    public void fileManager(JLabel label){
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int clickCount = e.getClickCount();
                if (clickCount >=1){
                    select.touch(label);
                }
            }
        });
    }

    // 快捷操作
    public void Shortcut(JLabel label){
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() >= 1){
                    select.touch(label);
                }
            }
        });
    }

    // 性能监控
    public void Monitor(JLabel label){
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() >= 1){
                    select.touch(label);
                }
            }
        });
    }

    // 连接设备
    public void setSelectDevice(JLabel label){
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() >= 1){
                    documentList.getConnectDevices();
                    setDevices.setConnectDevices();
                }
            }
        });
    }

    // ADB Monkey
    public void monkeyClick(JLabel label){
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int clickCount = e.getClickCount();
                if (clickCount >=1){
                    select.touch(label);
                }
            }
        });
    }


    public void closeDialog(){
        Thread thread = new Thread(){
            @Override
            public void run() {
                JFrame dialogFrame = component.getDialogFrame();
                try {
                    sleep(1800);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                dialogFrame.dispose();
            }
        };

        thread.start();
    }
}
